package io.renren.modules.app.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.form.FinanceWishdrawalsAppForm;
import io.renren.modules.finance.entity.FinanceMoneyRecord;
import io.renren.modules.finance.entity.FinanceWithdrawalsApp;
import io.renren.modules.finance.entity.FinanceWithdrawalsSetting;
import io.renren.modules.finance.entity.UserMoneyTotal;
import io.renren.modules.finance.service.FinanceMoneyRecordService;
import io.renren.modules.finance.service.FinanceWithdrawalsAppService;
import io.renren.modules.finance.service.FinanceWithdrawalsSettingService;
import io.renren.modules.finance.service.UserMoneyTotalService;
import io.renren.modules.user.entity.UserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @program: agent-server
 * @description: 财务- 收益账户
 * @author: 郭亚坤
 * @create: 2019-03-13 14:07
 **/
@RestController
@RequestMapping("/app/coin")
public class CoinController {

    @Autowired
    private FinanceWithdrawalsAppService financeWithdrawalsAppService;

    @Autowired
    private FinanceWithdrawalsSettingService financeWithdrawalsSettingService;

    @Autowired
    private UserMoneyTotalService agentMoneyTotalService;

    @Autowired
    private FinanceMoneyRecordService financeMoneyRecordService;

    /**
     * 代理申请提现
     *
     * @param map
     * @return
     */
    @PostMapping("getCoin")
    @Login
    public R withdraw(@LoginUser UserEntity user, @RequestBody Map map) {

        Integer type = (Integer) map.get("type");
        if (type == null) {
            type = 1;
        }
        int i = 0;
        int change_item = 0;
        String desc="";
        if (type == 2) {
            Integer r = new Random().nextInt(200);
            i = r + 1000;
            change_item= FinanceMoneyRecord.CHANGE_ITEM_COIN_2;
            desc="赚超额金币获取";
        } else {
            Integer r = new Random().nextInt(20);
            i = r + 100;
            change_item= FinanceMoneyRecord.CHANGE_ITEM_COIN_1;
            desc="赚金币获取";
        }

        boolean result = financeMoneyRecordService.saveRecord(Constant.CHANNEL_ID, user.getUserId(), change_item, desc, new BigDecimal(i), FinanceMoneyRecord.TYPE_COIN);
        if (result) {
            Map<String, Object> r = new HashMap<>();
            r.put("coin", i);
            return R.ok(r);
        } else {
            return R.error("请稍后重试");
        }
    }


}
